<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-05-06 15:01:17
 * @LastEditors: Sam
 * @LastEditTime: 2022-10-14 11:40:11
 */
namespace app\admin\controller;
use app\admin\model\{StoreCoupon,Goods,StoreCategory,StoreCouponIssue,StoreCouponIssueUser,StoreCouponUser};
use app\common\BaseController;

class Coupon extends BaseController{
    

    /**
     * 首页
     */
    public function index(){
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $where = [];
        if(isset($search["title"]) && $search["title"]!="") {
            $cid = StoreCoupon::where('title','like',"%{$search['title']}%")->column('id');
            $where[] = ['cid','in',$cid];
        }
        $query    = StoreCouponIssue::with('coupon')->where($where);
        $total    = $query->count();
        $data     = $query->page($page, $limit)->order('id desc')->append(['type_name','receive_date','publish_num','full_name','status_name'])->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total
        ]);
    }





        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-07-01
     */
    public function delete(){
        $id = param_check('ids');
        if(!$StoreCouponIssue = StoreCouponIssue::find($id)) json_response(0,'不存在');
        $StoreCouponIssue->is_del = 1;
        if($StoreCouponIssue->save()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    /**
     * 领取记录
     * @return [type] [description]
     */
    public function coupon_log(){
        $id = param_check('id');
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $where = [];
        $where[] = ['issue_coupon_id','=',$id];
        if(isset($search["title"]) && $search["title"]!="") $where[] = ["title","=",$search["title"]];
        $query    = StoreCouponIssueUser::with('user')->where($where)->order('add_time desc'); 
        $total    = $query->count();
        $data     = $query->page($page, $limit)->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total
        ]);
    }

    /**
     * 领取详情
     * @return [type] [description]
     */
    public function coupon_user(){
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $query    = StoreCouponUser::search_model($search); 
        $total    = $query->count();
        $data     = $query->page($page, $limit)->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total
        ]);
    }

}